package com.ugrow.ugrowcommunity.activity.web;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.ugrow.ugrowcommunity.databinding.ActivityWebBinding;

public class WebActivity extends AppCompatActivity {

    private ActivityWebBinding binding;
    protected String title;
    protected String url;
    private static final String TAG = "WebActivity";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityWebBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initEventAndData();
    }


    private void loadPDF1(){
        binding.webView.getSettings().setJavaScriptEnabled(true);
        binding.webView.getSettings().setSupportZoom(true);
        binding.webView.getSettings().setDomStorageEnabled(true);
        binding.webView.getSettings().setAllowFileAccess(true);
        binding.webView.getSettings().setPluginState(WebSettings.PluginState.ON);
        binding.webView.getSettings().setUseWideViewPort(true);
        binding.webView.getSettings().setBuiltInZoomControls(true);
        binding.webView.requestFocus();
        binding.webView.getSettings().setLoadWithOverviewMode(true);
        binding.webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        String pdfUrl = "http://82.157.173.219:9000/yinggu/%E5%85%B3%E4%BA%8E%E4%B8%BE%E5%8A%9E2021%E5%B9%B4%E9%AB%98%E6%A0%A1%E2%80%9C%E5%85%AC%E5%AF%93%E7%9A%84%E6%95%85%E4%BA%8B%E2%80%9D%E4%B8%BB%E9%A2%98%E7%B3%BB%E5%88%97%E6%B4%BB%E5%8A%A8%E7%9A%84%E9%80%9A%E7%9F%A5.pdf";
        binding.webView.loadUrl("http://docs.google.com/gview?embedded=true&url=" +pdfUrl);

    }

    private void loadPDF2(){
        binding.webView.getSettings().setJavaScriptEnabled(true);
        binding.webView.getSettings().setSupportZoom(true);
        binding.webView.getSettings().setDomStorageEnabled(true);
        binding.webView.getSettings().setAllowFileAccess(true);
        binding.webView.getSettings().setPluginState(WebSettings.PluginState.ON);
        binding.webView.getSettings().setUseWideViewPort(true);
        binding.webView.getSettings().setBuiltInZoomControls(true);
        binding.webView.requestFocus();
        binding.webView.getSettings().setLoadWithOverviewMode(true);
        binding.webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        String pdfUrl = "http://82.157.173.219:9000/yinggu/%E5%85%B3%E4%BA%8E%E4%B8%BE%E5%8A%9E2021%E5%B9%B4%E9%AB%98%E6%A0%A1%E2%80%9C%E5%85%AC%E5%AF%93%E7%9A%84%E6%95%85%E4%BA%8B%E2%80%9D%E4%B8%BB%E9%A2%98%E7%B3%BB%E5%88%97%E6%B4%BB%E5%8A%A8%E7%9A%84%E9%80%9A%E7%9F%A5.pdf";
        String data = "<iframe src='http://docs.google.com/gview?embedded=true&url="+pdfUrl+"'"+" width='100%' height='100%' style='border: none;'></iframe>";
        binding.webView.loadData(data, "text/html", "UTF-8");

    }

    protected void initEventAndData() {
        initWebView();

        if (getIntent() != null) {
            title = getIntent().getStringExtra("title");
            url = getIntent().getStringExtra("url");
            if (!TextUtils.isEmpty(title)) {
                binding.title.setText(title);
            }
            if (!TextUtils.isEmpty(url)) {
                binding.webView.loadUrl(url);
            }
        }
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void initWebView() {
        WebSettings mWebSettings = binding.webView.getSettings();
        mWebSettings.setSupportZoom(true);
        mWebSettings.setLoadWithOverviewMode(true);
        mWebSettings.setUseWideViewPort(true);
        mWebSettings.setDefaultTextEncodingName("utf-8");
        mWebSettings.setLoadsImagesAutomatically(true);
        mWebSettings.setBlockNetworkImage(false);//解决图片不显示
        mWebSettings.setTextZoom(100);//设置默认缩放比例，防止网页跟随系统字体大小变化

        //调用JS方法.安卓版本大于17,加上注解 @JavascriptInterface
        mWebSettings.setJavaScriptEnabled(true);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mWebSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }


        binding.webView.setWebChromeClient(webChromeClient);
        binding.webView.setWebViewClient(webViewClient);
        binding.back.setOnClickListener(v -> onLeftBackward());
    }

    WebViewClient webViewClient = new WebViewClient() {

        /**
         * 多页面在同一个WebView中打开，就是不新建activity或者调用系统浏览器打开
         */
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (isFinishing()) {
                return true;
            }
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageFinished(WebView webView, String s) {
            super.onPageFinished(webView, s);
            if (isFinishing()) {
                return;
            }

            Log.i(TAG, "onPageFinished() title=" + title);

            if (TextUtils.isEmpty(title)) {
                String title = webView.getTitle();
                if (!TextUtils.isEmpty(title)) {
                    binding.title.setText(title);
                }
            }
        }
    };

    WebChromeClient webChromeClient = new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView webView, int newProgress) {
            super.onProgressChanged(webView, newProgress);
            if (isFinishing()) {
                return;
            }

            if (newProgress == 100) {
                binding.progressBar.setVisibility(View.GONE);//加载完网页进度条消失
            } else {
                binding.progressBar.setVisibility(View.VISIBLE);//开始加载网页时显示进度条
                binding.progressBar.setProgress(newProgress);//设置进度值
            }


        }
    };

    private void onLeftBackward() {
        Log.i(TAG, "onLeftBackward()");
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            goBack();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }


    private void goBack() {
        if (binding.webView.canGoBack()) {
            binding.webView.goBack();
        } else {
            onLeftBackward();
        }
    }
}